<?php
namespace app\wxapp\controller;


class Lottery extends Base
{
    public function Lottery()
    {
global $_G, $_P;
$op = $_P['op'];
$themeid = $_P['themeid'];
if ($op == 'list') {
    $r = db_getall(t_clotteryrecode, array('tid' => $themeid));
    $ids = array();
    foreach ($r as &$v) {
        $open = explode(",",$v["users"]);
        $ids = array_merge($ids,$open);
        $v['users'] = $open;
        unset($v);
    }
    $ids = array_unique($ids);
    $virualUser = array_filter($ids,function($i){
        return strpos($i, 'vuser') === 0;
    });
    $params = str_replace("[","",json_encode(array_values($ids)));
    $params = str_replace("]","",$params);
    $params =  str_replace('"',"'",$params) ;
    $sql = "select nickname,avatar,openid,mobile from ". tablename(t_record) . " where status=1 and openid in (" . $params . ")"; 
    
    $gift = db_getall(t_clotterylist, array('tid' => $themeid));
    foreach ($gift as &$v) {
        $v['icon'] = tomedia($v['icon']);
        unset($v);
    }
    $users = db_select($sql);
    if(!empty($virualUser)){
        foreach ($virualUser as  &$v) {
            $v = str_replace('vuser', '', $v);
            unset($v);
        }
        $params = str_replace("[","",json_encode(array_values($virualUser)));
        $params = str_replace("]","",$params);
        $params =  str_replace('"',"'",$params) ;
        $sql = "select id,nickname,avatar,mobile from ". tablename(t_2019fans) . " where id in (" . $params . ")";
        $vuser = db_select($sql);
        foreach ($vuser as  $index => $u) {
            $vuser[$index]['openid'] = 'vuser' . $u['id'];
            unset($u);
        }
        $users = array_merge($users,$vuser);
    }

    foreach ($users as $index => $row) {
        $usernamelen=mb_strlen($row['mobile'],'utf-8');
        $users[$index]['mobile']=mb_substr($row['mobile'],0,3,'utf-8').'****'.mb_substr($row['mobile'],$usernamelen-4,4,'utf-8');
    }

    

    return $this->result(0, 'success', array('recode' => $r, 'gift' => $gift,'users'=>$users));
}

}
}